<?php
// +----------------------------------------------------------------------
// | Author: derek <693472824@qq.com> <http://www.bloveambition.com/>
// +----------------------------------------------------------------------

namespace Blovex4\Logic;

/**
 * 用户模型业务逻辑
 * @author derek <693472824@qq.com>
 */
class ActionLogic extends BaseLogic {

    /**
     * 记录行为日志
     * @param string $action 行为标识
     * @param string $model 触发行为的模型名
     * @param int $record_id 触发行为的记录id
     * @param int $user_id 执行行为的用户id
     * @return boolean
     * @author derek <693472824@qq.com>
     */
    public function action_log($action = null, $model = null, $record_id = null, $user_id = null){
        
        //参数检查
        if(empty($action) || empty($model) || empty($record_id)){
            return '参数不能为空';
        }
        
        if(empty($user_id)){
            $user_id = UserLogic::is_login();
        }
        
        $where['action_name'] = $action;
        
        //查询行为,判断是否执行
        $action_info = M('Action')->where($where)->find();
        if($action_info['action_status'] != 1){
            return '该行为被禁用或删除';
        }
        
        //插入行为日志
        $data['action_id']      =   $action_info['action_id'];
        $data['user_id']        =   $user_id;
        $data['action_ip']      =   ip2long(get_client_ip());
        $data['model']          =   $model;
        $data['record_id']      =   $record_id;
        $data['create_time']    =   NOW_TIME;
        
        //解析日志规则,生成日志备注
        if(!empty($action_info['action_log'])){
            if(preg_match_all('/\[(\S+?)\]/', $action_info['action_log'], $match)){
                $log['user']    =   $user_id;
                $log['record']  =   $record_id;
                $log['model']   =   $model;
                $log['time']    =   NOW_TIME;
                $log['data']    =   array('user'=>$user_id,'model'=>$model,'record'=>$record_id,'time'=>NOW_TIME);
                foreach ($match[1] as $value){
                    $param = explode('|', $value);
                    
                    if(isset($param[2])){//若 match 格式为 param|function|logic 
                           
                        $callback  = BIND_MODULE.'\\Logic\\'.$param[2].'Logic::'.$param[1];
                        $replace[] = call_user_func($callback, $log[$param[0]]);
                    
                    }elseif (isset($param[1])){//若 match 格式为 param|function
                           
                        $replace[] = call_user_func($param[1],$log[$param[0]]);
                    
                    }else{
                        $replace[] = $log[$param[0]];
                    }
                }
                $data['remark'] =   str_replace($match[0], $replace, $action_info['action_log']);
            }else{
                $data['remark'] =   $action_info['action_log'];
            }
        }else{
            //未定义日志规则，记录操作url
            $data['remark']     =   '操作url：'.$_SERVER['REQUEST_URI'];
        }
        
        M('ActionLog')->add($data);
        
    }
    
}

?>